<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="path" value="${pageContext.request.contextPath }" />
<html>
<head>
<script type="text/javascript" src="${path}/js/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="${path}/js/H-ui.admin.js"></script>
<link href="${path}/css/H-ui.css" rel="stylesheet" type="text/css" />
<link href="${path}/css/font/font-awesome.min.css" rel="stylesheet"
	type="text/css" />

<style type="text/css">
td:nth-child(odd) {
	height: 30px;
	line-height: 30px;
	text-align: right;
	padding-right: 30px;
}
</style>


</head>

<body>
	<div class="pd-20">
		<div>
			<img alt="" class="pd-20" src="${path }/images/logo_zking.png">
			<span class=" pd-20 f-24">代码生成器</span>
		</div>
		<form action="${path}/codegen/generatorUp" method="post" onsubmit="return checkSubmit();" >
			<table class="table table-border table-bordered table-bg">
				<tr>
					<td>设置数据库</td>
					<td><input style="width: 250px" type="text" name="url"
						id="url" placeholder="数据库URL" value="10.0.0.3:3306/db_"
						class="input-text"> <input style="width: 100px"
						type="text" name="userName" id="userName" placeholder="用户名"
						value="" class="input-text"> <input style="width: 100px"
						type="text" id="password" placeholder="密码" value=""
						name="password" class="input-text"> <input type="button"
						class="btn btn-success" onclick="getTableList()" value="加载数据库表" /></td>
				</tr>

				<tr>
					<td>设置数据库表</td>
					<td >
						<div id="tables" style="height: 500px;overflow: scroll;"  >
						</div>
					</td>
				</tr>
				<tr>
					<td>实体对象保存位置</td>
					<td><input class="input-text  auto" style="width: 350px"
						name="modelPackages[0]" placeholder="填写实体对象文件保存目录"> <input checked="checked"
						class="mr-30" type="checkbox" name="isCreateTemplateModelTypeStr[0]"
						value="JAVA_BEAN"></td>
				</tr>
				<tr>
					<td>设置dao文件模板</td>
					<td><input class="input-text  auto" style="width: 350px" 
						name="modelPackages[1]" placeholder="填写dao文件保存目录"> <input checked="checked"
						class="mr-30" type="checkbox" name="isCreateTemplateModelTypeStr[1]" value="MYBATIS_DAO"></td>
				</tr>

				<tr>
					<td>设置daoXml文件模板</td>
					<td><input class="input-text  auto" style="width: 350px" 
						name="modelPackages[2]" placeholder="填写daoXml文件保存目录"> <input checked="checked"
						class="mr-30" type="checkbox" name="isCreateTemplateModelTypeStr[2]"
						value="MYBATIS_DAO_IMPL"></td>
				</tr>

				<tr>
					<td>设置services接口文件模板</td>
					<td><input class="input-text  auto" style="width: 350px" 
						name="modelPackages[3]" placeholder="填写services接口文件保存目录">
						<input class="mr-30" type="checkbox" name="isCreateTemplateModelTypeStr[3]" checked="checked"
						value="SERVICE"></td>
				</tr>

				<tr>
					<td>设置services实现类文件模板</td>
					<td><input class="input-text  auto" style="width: 350px"
						name="modelPackages[4]" placeholder="填写services实现类文件保存目录">
						<input class="mr-30" type="checkbox" name="isCreateTemplateModelTypeStr[4]"  checked="checked"
						value="SERVICE_IMPL"></td>
				</tr>
				<tr>
					<td>设置action文件模板</td>
					<td><input class="input-text  auto" style="width: 350px" 
						name="modelPackages[5]" placeholder="填写action文件保存目录 "> <input checked="checked"
						class="mr-30" type="checkbox" name="isCreateTemplateModelTypeStr[5]"
						value="ACTION"></td>
				</tr>
				<tr>
					<td>设置视图文件模板</td>
					<td><input class="input-text  auto" style="width: 350px"  readonly="readonly"
						name="modelPackages[6]" placeholder="填写视图文件保存目录 -暂时没有对应模板"  > <input disabled="disabled"
						class="mr-30" type="checkbox" name="isCreateTemplateModelTypeStr[6]" value="VIEW"></td>
				</tr>


				<tr>
					<td>默认前缀：</td>
					<td><input id="file" class="input-text" style="width: 230px;">
						<input type="button" class="btn btn-success" onclick="setName()"
						value="自动命名文件夹" /></td>
				</tr>

				<tr>
					<td colspan="2" class="text-c"><input class="btn btn-primary"
						type="submit" value="开始生成模型" /></td>
				</tr>
			</table>
		</form>
	</div>
	<script type="text/javascript">
		function setName() {
			var name = document.getElementById("file").value;
			$("body").find(".auto").each(function(i) {
				var node = $(this);
				if (i == 0) {
					node.val(name + ".model");
				} else if (i == 1) {
					node.val(name + ".dao");
				} else if (i == 2) {
					node.val(name + ".dao.impl");
				} else if (i == 3) {
					node.val(name + ".services");
				} else if (i == 4) {
					node.val(name + ".services.impl");
				} else if (i == 5) {
					node.val(name + ".actions.admin");
				} else if (i == 6) {
					node.val(name + ".views");
				}

			});
			return false;
		}
		function getTableList() {
			if($("#userName").val().length<1 || 
					$("#url").val().length<1 ||
					$("#password").val().length<1 
					){
				alert('请先输入要查询的数据库？');
				return false;
			}	
			
			$
					.post(
							"${path}/codegen/getTableList",
							{
								url : $("#url").val(),
								userName : $("#userName").val(),
								password : $("#password").val()
							},
							function(data) {
								console.log(data);
								var html = "<span>";
								for (var i = 0; i < data.tables.length; i++) {
									html += '<label><i class="icon-table"></i> <input name="tablesName['+i+']" value='+data.tables[i]+' type="checkbox"> '
											+ data.tables[i] + ' </label><br>'
								}
								html += '</span><lable><i class="icon-table"></i> <input onclick="selectAll(this)" type="checkbox"> 全选</label><br>';
								$("#tables").html(html);
							});
			return false;
		}
		function selectAll(node) {
			$("#tables").find("span input").prop("checked", node.checked);
			return false;
		}
		function checkSubmit(){
			if($("#tables").find('label').length<1){
				alert('请先查询数据库表？');
				return false;
			}
		}
	</script>

</body>

</html>